// pages/user/user.js
var app = getApp();
let api = require("../../utils/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_pic:'../../images/user.jpg',
    user_name:'',
    array: ['男', '女'],
    user_js:'今天比昨天更加美好你说呢',
    user_photo:[
      {
        pic:'../../images/user.jpg'
      },
      {
        pic:'../../images/hzjm.png'
      },
      {
        pic:'../../images/hzjm.png'
      },
      {
        pic:'../../images/hzjm.png'
      },
      {
        pic:'../../images/hzjm.png'
      },
      {
        pic:'../../images/hzjm.png'
      },
    ],
    imgArr:[],
    lis:[]
  },
  upload () {
    var that=this;
    if (that.data.imgArr&&that.data.imgArr.length < 6) {
      wx.chooseMedia({
        count: 9,
        mediaType: ['image'],
        sourceType: ['album'],
        sizeType: ['original'],
        success(result){
          // console.log(result)
          that.setData({
            imgArr:result.tempFiles[0].tempFilePath
            })
          }
       })
    } else {
      wx.showToast({
        title: '最多上传1张图片',
        icon: 'none',
        duration: 3000
      });
    }
  },
  preview(event) {
    let currentUrl = event.currentTarget.dataset.src
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: this.data.imgArr // 需要预览的图片http链接列表
    })
  },
  deleteImg (e) {
    var that=this;
    var index = e.currentTarget.dataset.index;
    // console.log(index)
    // const pictureArr = that.data.imgArr[index].split('/')
    // that.data.imgArr.splice(index, 1)
    that.setData({
       imgArr:[]
    })
  },
  adInputdesc(e){
    this.setData({
      user_js: e.detail.value,    //与desc双向绑定
    })
    // console.log(this.data.user_js)
  },
  adInputname(e){
    this.setData({
      user_name: e.detail.value,    //与desc双向绑定
    })
  },
  submituserdetail(){
    var that =this
    var name = that.data.user_name
    var desc = that.data.user_js
    var token = wx.getStorageSync('token')
    const tempFilePaths = that.data.imgArr
    wx.uploadFile({
      url: api.UserDetail, //仅为示例，非真实的接口地址
      filePath: tempFilePaths,
      header:{'Authorization':'JWT'+' '+token},
      name: 'photo',
      formData: {
        name:name,
        desc:desc
      },
      success:(result)=>{
          if(result.data.code==100){
              that.setData({
                imgArr:res.data.photo_url,
                user_js:res.data.desc,
                user_name:res.data.name,
              })
              wx.navigateBack({
                delta:1
               })
          }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  bindPickerChange: function(e) {
    // this.setData({
    //   index: e.detail.value
    // })
    wx.showModal({     //显示模态对话框
      title: '禁止修改性别',
      content: '',
      showCancel: false,
      complete: (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {  //点确定
          
        }
      }
    })
  },
  onLoad(options) {
    let that=this;
    // console.log(this.data.imgArr)
    //获取本地缓存
    wx.getStorage({
      key:"user",
      success: function(res) {
        that.setData({
          lis:res.data
        })
      }
    })
    let token =wx.getStorageSync('token')
      wx.request({
        url: api.GetUserDetail,
        header: {
          'Authorization':'JWT'+' '+token // 请求头
       },
       success:result=>{
        if(result.data.code==100){console.log('eeeeeeeeeee')
          that.setData({
            imgArr:result.data.photo_url,
            user_js:result.data.desc,
            user_name:result.data.name,
          })
        }else{
          wx.navigateTo({
            url: "/pages/login/login",
          })
          
        }
       }
      })
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})